package cheqex.dao;
import org.apache.torque.util.Criteria;

import cheqex.update.controllers.DateController;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;


public class ReportPeer
    extends cheqex.dao.BaseReportPeer
{
	public static List<Report> retrieveAllReports(int reportId){
		Criteria criteria = new Criteria();
		criteria.add(ReportPeer.REPORTID,reportId);
		
		try{
			return (List<Report>)ReportPeer.doSelect(criteria);
		}catch(Exception e){
			e.printStackTrace();
			return new ArrayList<Report>();
		}
	}
	
	public static boolean isReportNull(int weekId){
		Criteria criteria = new Criteria();
		criteria.add(ReportPeer.REPORTID, weekId);
		try{
			if(ReportPeer.doSelect(criteria).isEmpty())
				return true;
			else return false;
		}catch (Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	public static boolean insertCashInBankForWeek(int weekId, int beginningBalance, int totalSumOfCollections, int bankInterest, int totalSumOfWithdrawals, int totalCashInBankBalance){
		Criteria criteria = new Criteria();
		criteria.add(ReportPeer.REPORTID, weekId);
		criteria.add(ReportPeer.BEGINNINGBALANCE, beginningBalance);
		criteria.add(ReportPeer.TOTALSUMOFCOLLECTIONS, totalSumOfCollections);
		criteria.add(ReportPeer.BANKINTEREST, bankInterest);
		criteria.add(ReportPeer.TOTALSUMOFWITHDRAWALS, totalSumOfWithdrawals);
		criteria.add(ReportPeer.TOTALCASHINBANKBALANCE, totalCashInBankBalance);
		
		if(isReportNull(weekId)==true){
			try{
				ReportPeer.doInsert(criteria);
				return true;
			}
			catch(Exception e){
				e.printStackTrace();
				return false;
			}
		}
		else{
			updateCashInBankForWeek(weekId,beginningBalance,totalSumOfCollections,bankInterest,totalSumOfWithdrawals,totalCashInBankBalance);
			return false;
		}
	}
	
	public static boolean updateCashInBankForWeek(int weekId, int beginningBalance, int totalSumOfCollections, int bankInterest, int totalSumOfWithdrawals, int totalCashInBankBalance){
		Criteria criteria = new Criteria();
		criteria.add(ReportPeer.BEGINNINGBALANCE, beginningBalance);
		criteria.add(ReportPeer.TOTALSUMOFCOLLECTIONS, totalSumOfCollections);
		criteria.add(ReportPeer.BANKINTEREST, bankInterest);
		criteria.add(ReportPeer.TOTALSUMOFWITHDRAWALS, totalSumOfWithdrawals);
		criteria.add(ReportPeer.TOTALCASHINBANKBALANCE, totalCashInBankBalance);
		
		Criteria scriteria = new Criteria();
		scriteria.add(ReportPeer.REPORTID, weekId);
		
		try{
			ReportPeer.doUpdate(scriteria, criteria);
			return true;
		}catch(Exception e){
			return false;
		}
	}
	
	public static boolean updateBankInterest(int weekId, int bankInterest){
		Criteria criteria = new Criteria();
		criteria.add(ReportPeer.BANKINTEREST, bankInterest);
		
		Criteria scriteria = new Criteria();
		scriteria.add(ReportPeer.REPORTID, weekId);
		
		try{
			ReportPeer.doUpdate(scriteria, criteria);
			return true;
		}catch(Exception e){
			return false;
		}
	}
}
